package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.Contract;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ContractService;
import com.xpsoft.oa.service.project.ProjectService;

public class PackageContractFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(PackageContractFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		//承包合同数据
		String packageContractProId = (String)pi.getVariable("packageContract.project.id");
		String constructScope = (String)pi.getVariable("packageContract.constructScope");
		String constructionUnit = (String)pi.getVariable("packageContract.constructionUnit");
		String mainIncludeUnit = (String)pi.getVariable("packageContract.mainIncludeUnit");
		String designUnit = (String)pi.getVariable("packageContract.designUnit");
		String consultUnit = (String)pi.getVariable("packageContract.consultUnit");
		BigDecimal packageContractAmount = (BigDecimal)pi.getVariable("packageContract.contractAmount");
		String supervisionUnit = (String)pi.getVariable("packageContract.supervisionUnit");
		String ownerRepresent = (String)pi.getVariable("packageContract.ownerRepresent");
		String projectLimit = (String)pi.getVariable("packageContract.projectLimit");
		String projectLimitProtocol = (String)pi.getVariable("packageContract.projectLimitProtocol");
		String progressPunishProtocol = (String)pi.getVariable("packageContract.progressPunishProtocol");
		String qualityRequire = (String)pi.getVariable("packageContract.qualityRequire");
		String qualityPunishProtocol = (String)pi.getVariable("packageContract.qualityPunishProtocol");
		String secureConstruct = (String)pi.getVariable("packageContract.secureConstruct");
		String balanceType = (String)pi.getVariable("packageContract.balanceType");
		String changeSignProtocol = (String)pi.getVariable("packageContract.changeSignProtocol");
		String balanceTimeProtocol = (String)pi.getVariable("packageContract.balanceTimeProtocol");
		String engineeringPay = (String)pi.getVariable("packageContract.engineeringPay");
		String mainDesignProtocol = (String)pi.getVariable("packageContract.mainDesignProtocol");
		String supplyProtocol = (String)pi.getVariable("packageContract.supplyProtocol");
		String otherDuty = (String)pi.getVariable("packageContract.otherDuty");
		String specialProtocol = (String)pi.getVariable("packageContract.specialProtocol");
		
		Contract contract = new Contract();
		//保存承包合同数据
		contract.setConstructScope(constructScope);
		contract.setConstructionUnit(constructionUnit);
		contract.setMainIncludeUnit(mainIncludeUnit);
		contract.setDesignUnit(designUnit);
		contract.setConsultUnit(consultUnit);
		contract.setSupervisionUnit(supervisionUnit);
		contract.setOwnerRepresent(ownerRepresent);
		contract.setSecureConstruct(secureConstruct);
		contract.setBalanceType(balanceType);
		contract.setChangeSignProtocol(changeSignProtocol);
		contract.setBalanceTimeProtocol(balanceTimeProtocol);
		contract.setEngineeringPay(engineeringPay);
		contract.setMainDesignProtocol(mainDesignProtocol);
		contract.setSupplyProtocol(supplyProtocol);
		contract.setOtherDuty(otherDuty);
		contract.setSpecialProtocol(specialProtocol);
		contract.setProjectLimit(projectLimit);
		contract.setProjectLimitProtocol(projectLimitProtocol);
		contract.setProgressPunishProtocol(progressPunishProtocol);
		contract.setQualityRequire(qualityRequire);
		contract.setQualityPunishProtocol(qualityPunishProtocol);
		contract.setContractAmount(packageContractAmount);
		contract.setType(Contract.PACKAGE_TYPE);//承包合同类型
		if(StringUtils.isNotBlank(packageContractProId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			contract.setProject(projectService.get(Long.valueOf(packageContractProId)));
		}
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		contract.setProcessRunId(processRun.getRunId());
		
		ContractService contractService = (ContractService)AppUtil.getBean("contractService");
		contractService.save(contract);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
